* Short support programs for the radar do-files
* such that they need not be placed on top of the dofile
* must place "do supportprograms"
* at the beginning of the do-file

************************************************************
*** SET THE STYLE FOR STATA GRAPHS
************************************************************
*** White style, without the ubiquitous blue frame area
grstyle init new, replace
grstyle color background white 

*****************************************************
*** 1. PROGRAM FOR PLOTTING 1 REGRESSION
*****************************************************
**************************
**** 1.1.BY WEEKS
**************************
capture program drop plotRegWeeks
program define plotRegWeeks
	args filename nameVar l1title ytitle  xtitle  note 
	global prew1minus = ${prew1}*-1

	preserve	
	clear
	use ${outputdir}/EVENT_FIG/parms.dta		
	***rename coefficients by weeks
	local wNum = ${prew1} + ${postw}
	display(`wNum')					
	rename parm week					
	local obsI = 0					
	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}					
	local obsIpost = ${prew1}					
	forvalues weekI = 1/${postw} {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}								
	keep in 1/`wNum'
	insobs 1			 
	replace week = "0" in -1
	replace estimate = 0 in -1					
	rename week week_string
	destring week_string, g(week)
	order week week_string
	sort week				

	***set options for graphs and labels 
	global main_option = "l1title(`l1title') ytitle(`ytitle')  yline(0) ylabel(#10, labsize(small)) xtitle(`xtitle') xline(0) xlabel(#28, labels labsize(small) ticks) xmtick(, labsize(tiny) ticks) legend(off) note(`note')"
	la var estimate "estimate `nameVar'"
	la var min95 "min95 `nameVar'"
	la var max95 "max95 `nameVar'"

	***output graph
	twoway (connected estimate week, lwidth(0.6) sort) (line min95 week, sort lcolor(gs9) lpattern(dash) cmissing(n)) (line max95 week, sort lcolor(gs9) lpattern(dash) cmissing(n)), ${main_option}
	graph save ${outputdir}/EVENT_FIG/`filename', replace
	*ytitle(`ytitle') l1title(`l1title') yline(0) ylabel(#10, labsize(small)) xtitle(`xtitle') xline(0) xlabel(#28, labels labsize(small) ticks) xmtick(, labsize(tiny) ticks) legend(off) note(`note')
	*${main_option}
	graph export ${outputdir}/EVENT_FIG/`filename'.png, replace width(1400)		

	***save the datafile under the graph name (withCI); erase intermediary .dta
	save ${outputdir}/EVENT_FIG/`filename', replace
	erase ${outputdir}/EVENT_FIG/parms.dta

	restore
end

**************************
**** 1.2. BY MONTHS
**************************
capture program drop plotRegMonths
program define plotRegMonths
	args filename nameVar l1title ytitle  xtitle  note 
	preserve

	global prem1minus = ${prem1}*-1
	clear
	use ${outputdir}/EVENT_FIG/parms.dta


	***rename coefficients by weeks

	local wNum = ${prem1} + ${postm}
	display(`wNum')

	rename parm month

	local obsI = 0

	forvalues monthI = ${prem1minus}/-1 {
		local ++obsI
		replace month = "`monthI'" in `obsI'
	}

	local obsIpost = ${prem1}

	forvalues monthI = 1/${postm} {
		local ++obsIpost
		replace month = "`monthI'" in `obsIpost'
	}			

	keep in 1/`wNum'
	insobs 1

	replace month = "0" in -1
	replace estimate = 0 in -1
	rename month month_string
	destring month_string, g(month)
	order month month_string
	sort month	


	***set options for graphs and labels 
	global main_option = "l1title(`l1title') ytitle(`ytitle')  yline(0) ylabel(#10, labsize(small)) xtitle(`xtitle') xline(0) xlabel(#28, labels labsize(small) ticks) xmtick(, labsize(tiny) ticks) legend(off) note(`note')"
	* 
	la var estimate "estimate `nameVar'"
	la var min95 "min95 `nameVar'"
	la var max95 "max95 `nameVar'"

	***output graph
	twoway (connected estimate month, lwidth(0.6) sort) (line min95 month, sort lcolor(gs9) lpattern(dash) cmissing(n)) (line max95 month, sort lcolor(gs9) lpattern(dash) cmissing(n)), ${main_option} 
	*ytitle(`ytitle') l1title(`l1title') yline(0) ylabel(#10, labsize(small)) xtitle(`xtitle') xline(0) xlabel(#28, labels labsize(small) ticks) xmtick(, labsize(tiny) ticks) legend(off) note(`note')
	*${main_option}
	graph save ${outputdir}/EVENT_FIG/`filename', replace
	graph export ${outputdir}/EVENT_FIG/`filename'.png, replace width(1400)


	***save the datafile under the graph name (withCI); erase intermediary .dta
	save ${outputdir}/EVENT_FIG/`filename', replace
	erase ${outputdir}/EVENT_FIG/parms.dta


	restore
end

**************************
**** 1.3. BY DRIVE-THROUGHS (drive number)
**************************
capture program drop plotRegDrives
program define plotRegDrives
	args filename nameVar l1title ytitle  xtitle  note 
	global predrvn1minus = ${predrvn1}*-1

	preserve	
	clear
	use ${outputdir}/EVENT_FIG/parms.dta



	***rename coefficients by drive-throughs

	local wNum = ${predrvn1} + ${postdrvn}
	display(`wNum')
	gen driveN = trim(substr(parm, 1, strpos(parm, ".") - 1)) if strpos(parm, "drvn5_reg")
	replace driveN = regexr(driveN, "[a-zA-Z]", "")	
	destring driveN, replace
	* this gives `drive number' on 1, 2, 3 ....
	replace driveN = driveN - 1000
	replace driveN = 0 in -1
	* while we need 1-5, 6-10, 11-12 and so on
	gen drive5 = driveN*5
	*** !!! We need to sort the labeling!
	*** WIth this, -5 represents drives -6 to -10
	*** 0 represents drives -1 to -5 etc 
	*** (in fact there ain't no drive zero)
	*replace drive5 = drive5-5 if drive5<=0


	drop parm

	* trim if needed
	drop if abs(drive5)>${predrvn1} & drive5<0
	drop if abs(drive5)>${postdrvn} & drive5>0				


	replace estimate = 0 in -1		
	replace min95 = . if driveN == 0
	replace max95 = . if driveN == 0
	sort drive5


	***set options for graphs and labels 
	global main_option = "l1title(`l1title') ytitle(`ytitle')  yline(0) ylabel(#10, labsize(small)) xtitle(`xtitle') xline(0) xlabel(#28, labels labsize(small) ticks) xmtick(, labsize(tiny) ticks) legend(off)"
	* 
	la var estimate "estimate `nameVar'"
	la var min95 "min95 `nameVar'"
	la var max95 "max95 `nameVar'"

	***output graph
	twoway (connected estimate drive5, lwidth(0.6) sort) (line min95 drive5, sort lcolor(gs9) lpattern(dash) cmissing(n)) (line max95 drive5, sort lcolor(gs9) lpattern(dash) cmissing(n)), ${main_option} note(`"`note'"') 
	graph save ${outputdir}/EVENT_FIG/`filename', replace

	*ytitle(`ytitle') l1title(`l1title') yline(0) ylabel(#10, labsize(small)) xtitle(`xtitle') xline(0) xlabel(#28, labels labsize(small) ticks) xmtick(, labsize(tiny) ticks) legend(off) note(`note')
	*${main_option}
	graph export ${outputdir}/EVENT_FIG/`filename'.png, replace width(1400)


	***save the datafile under the graph name (withCI); erase intermediary .dta
	save ${outputdir}/EVENT_FIG/`filename', replace
	erase ${outputdir}/EVENT_FIG/parms.dta


	restore
end

**************************
**** 1.4. BY WEEKS - LONG POST WINDOW OF 52 WEEKS
**************************
capture program drop plotRegLong
program define plotRegLong
	args filename nameVar l1title ytitle  xtitle  note 
	global prew1minus = ${prew1}*-1

	preserve	
	clear
	use ${outputdir}/EVENT_FIG/parms.dta

	***rename coefficients by weeks

	local wNum = ${prew1} + 52
	display(`wNum')

	rename parm week

	local obsI = 0

	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}

	local obsIpost = ${prew1}

	forvalues weekI = 1/52 {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}			

	keep in 1/`wNum'
	insobs 1

	replace week = "0" in -1
	replace estimate = 0 in -1					
	rename week week_string
	destring week_string, g(week)
	order week week_string
	sort week	


	***set options for graphs and labels 
	global main_option = "l1title(`l1title') ytitle(`ytitle')  yline(0) ylabel(#10, labsize(small)) xtitle(`xtitle') xline(0) xlabel(#28, labels labsize(small) ticks) xmtick(, labsize(tiny) ticks) legend(off) note(`note')"
	* 
	la var estimate "estimate `nameVar'"
	la var min95 "min95 `nameVar'"
	la var max95 "max95 `nameVar'"

	***output graph
	twoway (connected estimate week, lwidth(0.6) sort) (line min95 week, sort lcolor(gs9) lpattern(dash) cmissing(n)) (line max95 week, sort lcolor(gs9) lpattern(dash) cmissing(n)), ${main_option} 
	*ytitle(`ytitle') l1title(`l1title') yline(0) ylabel(#10, labsize(small)) xtitle(`xtitle') xline(0) xlabel(#28, labels labsize(small) ticks) xmtick(, labsize(tiny) ticks) legend(off) note(`note')
	*${main_option}
	graph save ${outputdir}/EVENT_FIG/`filename', replace
	graph export ${outputdir}/EVENT_FIG/`filename'.png, replace width(1400)


	***save the datafile under the graph name (withCI); erase intermediary .dta
	save ${outputdir}/EVENT_FIG/`filename', replace
	erase ${outputdir}/EVENT_FIG/parms.dta


	restore
end


*****************************************************
*** 2. PROGRAM FOR PLOTTING 2 REGRESSIONS
*****************************************************

**************************
**** 2.1 BY WEEKS
**************************
capture program drop plot2RegsWeeks
program define plot2RegsWeeks	
	args filename cat1 cat2 l1title ytitle xtitle note title

	global prew1minus = ${prew1}*-1
	
	save temp_plot, replace
	
	
	* make categories with underscores for variable renaming
	local cat1_varname = subinstr(strtrim(`"`cat1'"')," ","_",.)
	local cat2_varname = subinstr(strtrim(`"`cat2'"')," ","_",.)

	local wNum = ${prew1} + ${postw}
	display(`wNum')

	***merge parms from two regressions


	use ${outputdir}/EVENT_FIG/parms_cat2
	keep in 1/`wNum'
	*sufix for vars
	foreach var of varlist _all {
		rename `var' `var'_`cat2_varname'
	}	
	rename parm_`cat2_varname' week


	local obsI = 0

	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}

	local obsIpost = ${prew1}

	forvalues weekI = 1/${postw} {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}			

	insobs 1

	replace week = "0" in -1

	save, replace


	use ${outputdir}/EVENT_FIG/parms_cat1
	keep in 1/`wNum'
	*sufix for vars
	foreach var of varlist _all {
		rename `var' `var'_`cat1_varname'
	}
	rename parm_`cat1_varname' week


	local obsI = 0

	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}

	local obsIpost = ${prew1}

	forvalues weekI = 1/${postw} {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}			

	insobs 1

	replace week = "0" in -1

	save, replace

	merge 1:1 week using ${outputdir}/EVENT_FIG/parms_cat2

	***rename coefficients by weeks



	replace estimate_`cat1_varname' = 0 if week=="0"
	replace estimate_`cat2_varname' = 0 if week=="0"
	rename week week_string
	destring week_string, g(week)
	order week week_string
	sort week	


	***set options for graphs and labels 
	global main_option = "title(`title') l1title(`l1title') ytitle(`ytitle') yline(0) ylabel(#10, labsize(small)) xtitle(`xtitle') xline(0) xlabel(#28, labels labsize(small) ticks) xmtick(, labsize(tiny) ticks) legend(order(1 2)) note(`note')"

	global opt_cat1 = "sort lcolor(black) lwidth(0.6) msymbol(O)"
	global opt_cat2 = "sort lcolor(red) lwidth(0.6) msymbol(T)"

	global CI_cat1 "sort lcolor(black) lpattern(shortdash) lwidth(0.2) cmissing(n)"
	global CI_cat2 "sort lcolor(red)   lpattern(shortdash) lwidth(0.2) cmissing(n)"


	la var estimate_`cat1_varname' "`cat1'"
	la var estimate_`cat2_varname' "`cat2'"
	*	la var min95_`cat1' "min95 `cat1'"
	*	la var max95_`cat1' "max95 `cat1'"
	*	la var min95_`cat2' "min95 `cat2'"
	*	la var max95_`cat2' "max95 `cat2'"

	***create graphs

	twoway (connected estimate_`cat1_varname' week,${opt_cat1} ) (connected estimate_`cat2_varname' week, ${opt_cat2}) (line min95_`cat1_varname' week, ${CI_cat1}) (line max95_`cat1_varname' week, ${CI_cat1}) (line min95_`cat2_varname' week, ${CI_cat2}) (line max95_`cat2_varname' week, ${CI_cat2}), ${main_option}     
	graph save ${outputdir}/EVENT_FIG/`filename'_ci, replace
	graph export ${outputdir}/EVENT_FIG/`filename'_ci.png, replace width(1400)

	twoway (connected estimate_`cat1_varname' week, ${opt_cat1}) (connected estimate_`cat2_varname' week, ${opt_cat2}), ${main_option} 
	graph save ${outputdir}/EVENT_FIG/`filename'_noci, replace
	graph export ${outputdir}/EVENT_FIG/`filename'_noci.png, replace width(1400)


	***save the datafile under the graph name (withCI); erase intermediary .dta
	save ${outputdir}/EVENT_FIG/`filename', replace	
	erase ${outputdir}/EVENT_FIG/parms_cat1.dta
	erase ${outputdir}/EVENT_FIG/parms_cat2.dta	
	
	use temp_plot.dta
	cap erase temp_plot.dta

	

end


*****************************************************
*** 3. PROGRAM FOR PLOTTING 3 REGRESSIONS
*****************************************************

**************************
**** 3.1. BY WEEKS
**************************
capture program drop plot3RegsWeeks
program define plot3RegsWeeks	
	args filename cat1 cat2 cat3 l1title ytitle xtitle note 

	global prew1minus = ${prew1}*-1

	preserve

	local cat1_varname = subinstr(strtrim(`"`cat1'"')," ","_",.)
	local cat2_varname = subinstr(strtrim(`"`cat2'"')," ","_",.)
	local cat3_varname = subinstr(strtrim(`"`cat3'"')," ","_",.)


	local wNum = ${prew1} + ${postw}
	display(`wNum')

	***merge parms from three regressions
	*** 1.
	use ${outputdir}/EVENT_FIG/parms_cat1
	keep in 1/`wNum'
	*sufix for vars
	foreach var of varlist _all {
		rename `var' `var'_`cat1_varname'
	}
	rename parm_`cat1_varname' week


	local obsI = 0

	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}

	local obsIpost = ${prew1}

	forvalues weekI = 1/${postw} {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}			

	insobs 1

	replace week = "0" in -1

	save, replace
	*** 2.
	use ${outputdir}/EVENT_FIG/parms_cat2
	keep in 1/`wNum'
	*sufix for vars
	foreach var of varlist _all {
		rename `var' `var'_`cat2_varname'
	}	
	rename parm_`cat2_varname' week

	local obsI = 0

	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}

	local obsIpost = ${prew1}

	forvalues weekI = 1/${postw} {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}			

	insobs 1

	replace week = "0" in -1

	save, replace

	*** 3.
	use ${outputdir}/EVENT_FIG/parms_cat3
	keep in 1/`wNum'
	*sufix for vars
	foreach var of varlist _all {
		rename `var' `var'_`cat3_varname'
	}	
	rename parm_`cat3_varname' week

	local obsI = 0

	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}

	local obsIpost = ${prew1}

	forvalues weekI = 1/${postw} {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}			

	insobs 1

	replace week = "0" in -1

	save, replace


	*** merge in
	merge 1:1 week using ${outputdir}/EVENT_FIG/parms_cat2
	drop _merge
	merge 1:1 week using ${outputdir}/EVENT_FIG/parms_cat1

	***rename coefficients by weeks				
	replace estimate_`cat1_varname' = 0 if week=="0"
	replace estimate_`cat2_varname' = 0 if week=="0"
	replace estimate_`cat3_varname' = 0 if week=="0"
	rename week week_string
	destring week_string, g(week)
	order week week_string
	sort week	


	***set options for graphs and labels 
	global main_option = "l1title(`l1title') ytitle(`ytitle') yline(0) ylabel(#10, labsize(small)) xtitle(`xtitle') xline(0) xlabel(#28, labels labsize(small) ticks) xmtick(, labsize(tiny) ticks) legend(order(1 2 3) r(1)) note(`note')"

	global opt_cat1 = "sort lcolor(red) lwidth(0.6)  msymbol(O) mcolor(red)"
	global opt_cat2 = "sort lcolor(blue) lwidth(0.6)  msymbol(T) mcolor(blue)"
	global opt_cat3 = "sort lcolor(black) lwidth(0.6)  msymbol(D) mcolor(black)"


	global CI_cat1 "sort lcolor(red) lpattern(shortdash) lwidth(0.2) cmissing(n)"
	global CI_cat2 "sort lcolor(blue)   lpattern(shortdash) lwidth(0.2) cmissing(n)"
	global CI_cat3 "sort lcolor(black)   lpattern(shortdash) lwidth(0.2) cmissing(n)"


	la var estimate_`cat1_varname' "`cat1'"
	la var estimate_`cat2_varname' "`cat2'"
	la var estimate_`cat3_varname' "`cat3'"

	*	la var min95_`cat1' "min95 `cat1'"
	*	la var max95_`cat1' "max95 `cat1'"
	*	la var min95_`cat2' "min95 `cat2'"
	*	la var max95_`cat2' "max95 `cat2'"

	***create graphs	
	#delimit ;
	twoway (connected estimate_`cat1_varname' week,${opt_cat1} ) 
	(connected estimate_`cat2_varname' week, ${opt_cat2}) 
	(connected estimate_`cat3_varname' week, ${opt_cat3}) 
	(line min95_`cat1_varname' week, ${CI_cat1})
	(line max95_`cat1_varname' week, ${CI_cat1}) 
	(line min95_`cat2_varname' week, ${CI_cat2}) 
	(line max95_`cat2_varname' week, ${CI_cat2}) 
	(line min95_`cat3_varname' week, ${CI_cat3}) 
	(line max95_`cat3_varname' week, ${CI_cat3}) 
	, ${main_option}; 	   
	#delimit cr
	graph save ${outputdir}/EVENT_FIG/`filename'_ci, replace
	graph export ${outputdir}/EVENT_FIG/`filename'_ci.png, replace width(1400)
	#delimit ;
	twoway (connected estimate_`cat1_varname' week, ${opt_cat1}) 
	(connected estimate_`cat2_varname' week, ${opt_cat2})
	(connected estimate_`cat3_varname' week, ${opt_cat3}), ${main_option};
	#delimit cr
	graph save ${outputdir}/EVENT_FIG/`filename'_noci, replace
	graph export ${outputdir}/EVENT_FIG/`filename'_noci.png, replace width(1400)


	***save the datafile under the graph name (withCI); erase intermediary .dta
	save ${outputdir}/EVENT_FIG/`filename', replace	
	erase ${outputdir}/EVENT_FIG/parms_cat1.dta
	erase ${outputdir}/EVENT_FIG/parms_cat2.dta
	erase ${outputdir}/EVENT_FIG/parms_cat3.dta
	restore

end

*****************************************************
*** 4. PROGRAM FOR PLOTTING 4 REGRESSIONS
*****************************************************

**************************
**** 4.1. BY WEEKS
**************************
capture program drop plot4RegsWeeks
program define plot4RegsWeeks	
	args filename cat1 cat2 cat3 cat4 l1title ytitle xtitle note 

	global prew1minus = ${prew1}*-1

	preserve

	local cat1_varname = "c1"
	local cat2_varname = "c2"
	local cat3_varname = "c3"
	local cat4_varname = "c4"


	local wNum = ${prew1} + ${postw}
	display(`wNum')

	***merge parms from three regressions
	*** 1.
	use ${outputdir}/EVENT_FIG/parms_cat1
	keep in 1/`wNum'
	*sufix for vars
	foreach var of varlist _all {
		rename `var' `var'_`cat1_varname'
	}
	rename parm_`cat1_varname' week


	local obsI = 0

	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}

	local obsIpost = ${prew1}

	forvalues weekI = 1/${postw} {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}			

	insobs 1

	replace week = "0" in -1

	save, replace
	*** 2.
	use ${outputdir}/EVENT_FIG/parms_cat2
	keep in 1/`wNum'
	*sufix for vars
	foreach var of varlist _all {
		rename `var' `var'_`cat2_varname'
	}	
	rename parm_`cat2_varname' week

	local obsI = 0

	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}

	local obsIpost = ${prew1}

	forvalues weekI = 1/${postw} {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}			

	insobs 1

	replace week = "0" in -1

	save, replace

	*** 3.
	use ${outputdir}/EVENT_FIG/parms_cat3
	keep in 1/`wNum'
	*sufix for vars
	foreach var of varlist _all {
		rename `var' `var'_`cat3_varname'
	}	
	rename parm_`cat3_varname' week

	local obsI = 0

	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}

	local obsIpost = ${prew1}

	forvalues weekI = 1/${postw} {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}			

	insobs 1

	replace week = "0" in -1

	save, replace

	*** 4.
	use ${outputdir}/EVENT_FIG/parms_cat4
	keep in 1/`wNum'
	*sufix for vars
	foreach var of varlist _all {
		rename `var' `var'_`cat4_varname'
	}	
	rename parm_`cat4_varname' week

	local obsI = 0

	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}

	local obsIpost = ${prew1}

	forvalues weekI = 1/${postw} {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}			

	insobs 1

	replace week = "0" in -1

	save, replace


	*** merge in
	merge 1:1 week using ${outputdir}/EVENT_FIG/parms_cat3
	drop _merge
	merge 1:1 week using ${outputdir}/EVENT_FIG/parms_cat2
	drop _merge
	merge 1:1 week using ${outputdir}/EVENT_FIG/parms_cat1

	***rename coefficients by weeks				
	replace estimate_`cat1_varname' = 0 if week=="0"
	replace estimate_`cat2_varname' = 0 if week=="0"
	replace estimate_`cat3_varname' = 0 if week=="0"
	replace estimate_`cat4_varname' = 0 if week=="0"

	rename week week_string
	destring week_string, g(week)
	order week week_string
	sort week	


	***set options for graphs and labels 
	global main_option = "l1title(`l1title') ytitle(`ytitle', margin(0 10 0 0)) yline(0) ylabel(#10, labsize(small)) xtitle(`xtitle') xline(0) xlabel(#28, labels labsize(small) ticks) xmtick(, labsize(tiny) ticks) legend(order(1 2 3 4) r(1)) note(`note')"

	global opt_cat1 = "sort lcolor(black) lwidth(0.6) msymbol(O)"
	global opt_cat2 = "sort lcolor(red) lwidth(0.6) msymbol(T)"
	global opt_cat3 = "sort lcolor(blue) lwidth(0.6) msymbol(D)"
	global opt_cat4 = "sort lcolor(green) lwidth(0.6) msymbol(S)"


	global CI_cat1 "sort lcolor(black) lpattern(shortdash) lwidth(0.2) cmissing(n)"
	global CI_cat2 "sort lcolor(red)   lpattern(shortdash) lwidth(0.2) cmissing(n)"
	global CI_cat3 "sort lcolor(blue)   lpattern(shortdash) lwidth(0.2) cmissing(n)"
	global CI_cat4 "sort lcolor(green)   lpattern(shortdash) lwidth(0.2) cmissing(n)"


	la var estimate_`cat1_varname' "`cat1'"
	la var estimate_`cat2_varname' "`cat2'"
	la var estimate_`cat3_varname' "`cat3'"
	la var estimate_`cat4_varname' "`cat4'"

	*	la var min95_`cat1' "min95 `cat1'"
	*	la var max95_`cat1' "max95 `cat1'"
	*	la var min95_`cat2' "min95 `cat2'"
	*	la var max95_`cat2' "max95 `cat2'"

	***create graphs	
	#delimit ;
	twoway (connected estimate_`cat1_varname' week,${opt_cat1} ) 
	(connected estimate_`cat2_varname' week, ${opt_cat2}) 
	(connected estimate_`cat3_varname' week, ${opt_cat3}) 
	(connected estimate_`cat4_varname' week, ${opt_cat4}) 

	(line min95_`cat1_varname' week, ${CI_cat1})
	(line max95_`cat1_varname' week, ${CI_cat1}) 
	(line min95_`cat2_varname' week, ${CI_cat2}) 
	(line max95_`cat2_varname' week, ${CI_cat2}) 
	(line min95_`cat3_varname' week, ${CI_cat3}) 
	(line max95_`cat3_varname' week, ${CI_cat3}) 
	(line min95_`cat4_varname' week, ${CI_cat4}) 
	(line max95_`cat4_varname' week, ${CI_cat4}) 
	, ${main_option} ; 	   
	#delimit cr
	graph save ${outputdir}/EVENT_FIG/`filename'_ci, replace
	graph export ${outputdir}/EVENT_FIG/`filename'_ci.png, replace width(1400)
	#delimit ;

	twoway (connected estimate_`cat1_varname' week, ${opt_cat1}) 
	(connected estimate_`cat2_varname' week, ${opt_cat2})
	(connected estimate_`cat3_varname' week, ${opt_cat3})
	(connected estimate_`cat4_varname' week, ${opt_cat4}), ${main_option};
	#delimit cr
	graph save ${outputdir}/EVENT_FIG/`filename'_noci, replace
	graph export ${outputdir}/EVENT_FIG/`filename'_noci.png, replace width(1400)


	***save the datafile under the graph name (withCI); erase intermediary .dta
	save ${outputdir}/EVENT_FIG/`filename', replace	
	erase ${outputdir}/EVENT_FIG/parms_cat1.dta
	erase ${outputdir}/EVENT_FIG/parms_cat2.dta
	erase ${outputdir}/EVENT_FIG/parms_cat3.dta
	erase ${outputdir}/EVENT_FIG/parms_cat4.dta

	restore

end

*****************************************************
*** 5 . PROGRAM FOR PLOTTING 5 REGRESSIONS
*****************************************************

**************************
**** 5.1. BY WEEKS
**************************
capture program drop plot5RegsWeeks
program define plot5RegsWeeks	
	args filename cat1 cat2 cat3 cat4 cat5 l1title ytitle xtitle note 
	
	global prew1minus = ${prew1}*-1

	save temp_plot, replace

	local cat1_varname = "c1"
	local cat2_varname = "c2"
	local cat3_varname = "c3"
	local cat4_varname = "c4"
	local cat5_varname = "c5"

	local wNum = ${prew1} + ${postw}
	display(`wNum')

	***merge parms from three regressions
	*** 1.
	use ${outputdir}/EVENT_FIG/parms_cat1
	keep in 1/`wNum'
	*sufix for vars
	foreach var of varlist _all {
		rename `var' `var'_`cat1_varname'
	}
	rename parm_`cat1_varname' week


	local obsI = 0

	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}

	local obsIpost = ${prew1}

	forvalues weekI = 1/${postw} {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}			

	insobs 1

	replace week = "0" in -1

	save, replace
	*** 2.
	use ${outputdir}/EVENT_FIG/parms_cat2
	keep in 1/`wNum'
	*sufix for vars
	foreach var of varlist _all {
		rename `var' `var'_`cat2_varname'
	}	
	rename parm_`cat2_varname' week

	local obsI = 0

	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}

	local obsIpost = ${prew1}

	forvalues weekI = 1/${postw} {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}			

	insobs 1

	replace week = "0" in -1

	save, replace

	*** 3.
	use ${outputdir}/EVENT_FIG/parms_cat3
	keep in 1/`wNum'
	*sufix for vars
	foreach var of varlist _all {
		rename `var' `var'_`cat3_varname'
	}	
	rename parm_`cat3_varname' week

	local obsI = 0

	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}

	local obsIpost = ${prew1}

	forvalues weekI = 1/${postw} {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}			

	insobs 1

	replace week = "0" in -1

	save, replace

	*** 4.
	use ${outputdir}/EVENT_FIG/parms_cat4
	keep in 1/`wNum'
	*sufix for vars
	foreach var of varlist _all {
		rename `var' `var'_`cat4_varname'
	}	
	rename parm_`cat4_varname' week

	local obsI = 0

	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}

	local obsIpost = ${prew1}

	forvalues weekI = 1/${postw} {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}			

	insobs 1

	replace week = "0" in -1

	save, replace
	
	*** 5.
	use ${outputdir}/EVENT_FIG/parms_cat5
	keep in 1/`wNum'
	*sufix for vars
	foreach var of varlist _all {
		rename `var' `var'_`cat5_varname'
	}	
	rename parm_`cat5_varname' week

	local obsI = 0

	forvalues weekI = ${prew1minus}/-1 {
		local ++obsI
		replace week = "`weekI'" in `obsI'
	}

	local obsIpost = ${prew1}

	forvalues weekI = 1/${postw} {
		local ++obsIpost
		replace week = "`weekI'" in `obsIpost'
	}			

	insobs 1

	replace week = "0" in -1

	save, replace


	*** merge in
	merge 1:1 week using ${outputdir}/EVENT_FIG/parms_cat4
	drop _merge
	merge 1:1 week using ${outputdir}/EVENT_FIG/parms_cat3
	drop _merge
	merge 1:1 week using ${outputdir}/EVENT_FIG/parms_cat2
	drop _merge
	merge 1:1 week using ${outputdir}/EVENT_FIG/parms_cat1

	***rename coefficients by weeks				
	replace estimate_`cat1_varname' = 0 if week=="0"
	replace estimate_`cat2_varname' = 0 if week=="0"
	replace estimate_`cat3_varname' = 0 if week=="0"
	replace estimate_`cat4_varname' = 0 if week=="0"
	replace estimate_`cat5_varname' = 0 if week=="0"
	
	rename week week_string
	destring week_string, g(week)
	order week week_string
	sort week	


	***set options for graphs and labels 
	global main_option = "l1title(`l1title') ytitle(`ytitle', margin(0 20 0 0)) yline(0) ylabel(#10, labsize(vsmall)) xtitle(`xtitle') xline(0) xlabel(#28, labels labsize(small) ticks) xmtick(, labsize(tiny) ticks) legend(order(1 2 3 4 5)  size(tiny) r(1)) note(`note')"

	global opt_cat1 = "sort lcolor(black) lwidth(0.6) msymbol(O)"
	global opt_cat2 = "sort lcolor(red) lwidth(0.6) msymbol(T)"
	global opt_cat3 = "sort lcolor(blue) lwidth(0.6) msymbol(D)"
	global opt_cat4 = "sort lcolor(green) lwidth(0.6) msymbol(S)"
	global opt_cat5 = "sort lcolor(purple) lwidth(0.6) msymbol(+)"


	global CI_cat1 "sort lcolor(black) lpattern(shortdash) lwidth(0.2) cmissing(n)"
	global CI_cat2 "sort lcolor(red)   lpattern(shortdash) lwidth(0.2) cmissing(n)"
	global CI_cat3 "sort lcolor(blue)   lpattern(shortdash) lwidth(0.2) cmissing(n)"
	global CI_cat4 "sort lcolor(green)   lpattern(shortdash) lwidth(0.2) cmissing(n)"
	global CI_cat5 "sort lcolor(purple)   lpattern(shortdash) lwidth(0.2) cmissing(n)"

	la var estimate_`cat1_varname' "`cat1'"
	la var estimate_`cat2_varname' "`cat2'"
	la var estimate_`cat3_varname' "`cat3'"
	la var estimate_`cat4_varname' "`cat4'"
	la var estimate_`cat5_varname' "`cat5'"
	
	*	la var min95_`cat1' "min95 `cat1'"
	*	la var max95_`cat1' "max95 `cat1'"
	*	la var min95_`cat2' "min95 `cat2'"
	*	la var max95_`cat2' "max95 `cat2'"

	***create graphs	
	#delimit ;
	twoway (connected estimate_`cat1_varname' week,${opt_cat1} ) 
	(connected estimate_`cat2_varname' week, ${opt_cat2}) 
	(connected estimate_`cat3_varname' week, ${opt_cat3}) 
	(connected estimate_`cat4_varname' week, ${opt_cat4}) 
	(connected estimate_`cat5_varname' week, ${opt_cat5}) 

	(line min95_`cat1_varname' week, ${CI_cat1})
	(line max95_`cat1_varname' week, ${CI_cat1}) 
	(line min95_`cat2_varname' week, ${CI_cat2}) 
	(line max95_`cat2_varname' week, ${CI_cat2}) 
	(line min95_`cat3_varname' week, ${CI_cat3}) 
	(line max95_`cat3_varname' week, ${CI_cat3}) 
	(line min95_`cat4_varname' week, ${CI_cat4}) 
	(line max95_`cat4_varname' week, ${CI_cat4}) 
	(line min95_`cat5_varname' week, ${CI_cat5}) 
	(line max95_`cat5_varname' week, ${CI_cat5}) 
	, ${main_option} ; 	   
	#delimit cr
	graph save ${outputdir}/EVENT_FIG/`filename'_ci, replace
	graph export ${outputdir}/EVENT_FIG/`filename'_ci.png, replace width(1400)
	#delimit ;

	twoway (connected estimate_`cat1_varname' week, ${opt_cat1}) 
	(connected estimate_`cat2_varname' week, ${opt_cat2})
	(connected estimate_`cat3_varname' week, ${opt_cat3})
	(connected estimate_`cat4_varname' week, ${opt_cat4})
	(connected estimate_`cat5_varname' week, ${opt_cat5})
	, ${main_option};
	#delimit cr
	graph save ${outputdir}/EVENT_FIG/`filename'_noci, replace
	graph export ${outputdir}/EVENT_FIG/`filename'_noci.png, replace width(1400)


	***save the datafile under the graph name (withCI); erase intermediary .dta
	save ${outputdir}/EVENT_FIG/`filename', replace	
	
	erase ${outputdir}/EVENT_FIG/parms_cat1.dta
	erase ${outputdir}/EVENT_FIG/parms_cat2.dta
	erase ${outputdir}/EVENT_FIG/parms_cat3.dta
	erase ${outputdir}/EVENT_FIG/parms_cat4.dta
	erase ${outputdir}/EVENT_FIG/parms_cat5.dta	
	
	use temp_plot.dta
	cap erase temp_plot.dta

end


** Remove Czech Symbols, Trim leading and trailing spaces, make lowercase
cap program drop sanitize_string
program define sanitize_string
	args string_var

	* remove diakritika from all vars to keep compatibility if other dofiles and Stata 13
	local what = "á Á č Č ď Ď é É ě Ě í Í ň Ň ó Ó ř Ř š Š ť Ť ú Ú ů Ů ý Ý ž Ž Ä ä Ľ ľ Ô ô ò"
	local with = "a A c C d D e E e E i I n N o O r R s S t T u U u U y Y z Z A a L l O o n" 
	local n1 : word count `what'
	local n2 : word count `with' 
	if `n1'==`n2' {		
		forvalues i = 1/`n1' {
			local a : word `i' of `what'
			local b : word `i' of `with'			
			cap replace `string_var' = subinstr(`string_var',`"`a'"',`"`b'"',.)
		}				
	} 
	else {
		di `unequal # of elements on the lists, error!'
	}
	* trim
	replace `string_var' = trim(`string_var')
	* make lowercase
	replace `string_var' = lower(`string_var')
end

* program for parsin date variables, to have logic on one place
* for DMY dates (old fines)
cap program drop parse_date
program define parse_date
	args newvar_name parsed_datevar
	* sanitize string
	replace `parsed_datevar' = strtrim(`parsed_datevar')
	g `newvar_name'=date(`parsed_datevar', "DMY hms")
	replace `newvar_name'=date(`parsed_datevar', "DMY") if `newvar_name'==.
	replace `newvar_name'=date(`parsed_datevar', "DMY hm") if `newvar_name'==.	
	format %td `newvar_name'
	* check if all strings present as date are parsed
	g check`newvar_name' = (`parsed_datevar'!= "" & `newvar_name'==.)
	assert check`newvar_name' == 0
	drop check`newvar_name'
end
* program for MDY dates (new fines)
cap program drop parse_date2
program define parse_date2
	args newvar_name parsed_datevar
	* sanitize string
	replace `parsed_datevar' = strtrim(`parsed_datevar')
	replace `parsed_datevar' = subinstr(`parsed_datevar', "/", "-", .)
	replace `parsed_datevar' = subinstr(`parsed_datevar', "T", " ", .)	
	replace `parsed_datevar' = word(`parsed_datevar',1)
	g `newvar_name'=date(`parsed_datevar', "MDY hms")
	replace `newvar_name'=date(`parsed_datevar', "MDY") if `newvar_name'==.
	replace `newvar_name'=date(`parsed_datevar', "MDY hm") if `newvar_name'==.
	replace `newvar_name'=date(`parsed_datevar', "MDY hm s") if `newvar_name'==.
	replace `newvar_name'=date(`parsed_datevar', "YMD") if `newvar_name'==.
	format %td `newvar_name'
	* check if all strings present as date are parsed
	g check`newvar_name' = (`parsed_datevar'!= "" & `newvar_name'==.)
	assert check`newvar_name' == 0
	drop check`newvar_name'
end


* program for parsin date variables, to have logic on one place
* for DMY dates (old fines)
cap program drop parse_date
program define parse_date
	args newvar_name parsed_datevar
	* sanitize string
	replace `parsed_datevar' = strtrim(`parsed_datevar')
	g `newvar_name'=date(`parsed_datevar', "DMY hms")
	replace `newvar_name'=date(`parsed_datevar', "DMY") if `newvar_name'==.
	replace `newvar_name'=date(`parsed_datevar', "DMY hm") if `newvar_name'==.	
	format %td `newvar_name'
	* check if all strings present as date are parsed
	g check`newvar_name' = (`parsed_datevar'!= "" & `newvar_name'==.)
	assert check`newvar_name' == 0
	drop check`newvar_name'
end
* program for MDY dates (new fines)
cap program drop parse_date2
program define parse_date2
	args newvar_name parsed_datevar
	* sanitize string
	replace `parsed_datevar' = strtrim(`parsed_datevar')
	replace `parsed_datevar' = subinstr(`parsed_datevar', "/", "-", .)
	replace `parsed_datevar' = subinstr(`parsed_datevar', "T", " ", .)	
	replace `parsed_datevar' = word(`parsed_datevar',1)
	g `newvar_name'=date(`parsed_datevar', "MDY hms")
	replace `newvar_name'=date(`parsed_datevar', "MDY") if `newvar_name'==.
	replace `newvar_name'=date(`parsed_datevar', "MDY hm") if `newvar_name'==.
	replace `newvar_name'=date(`parsed_datevar', "MDY hm s") if `newvar_name'==.
	replace `newvar_name'=date(`parsed_datevar', "YMD") if `newvar_name'==.
	replace `newvar_name'=date(`parsed_datevar', "DMY") if `newvar_name'==.
	format %td `newvar_name'
	* check if all strings present as date are parsed
	g check`newvar_name' = (`parsed_datevar'!= "" & `newvar_name'==.)
	assert check`newvar_name' == 0
	drop check`newvar_name'
end
